<?php
	class BlogModel extends Model{
		public $page;		//分页的页号(当前页)
		public $evelpage=8;	//每页显示两条记录。
		public $pagenum;	//总共的页数
		public $bplist;		//输出到模版的每条记录的数据
		public $pagefor;	//显示10个页号标签
		public $count;		//总记录数
		public $tags;       //标签
		public function __construct($page){
			$this->page = $page;
			$this->pagelist();
			$this->pagefor = $this->getpagefor();
		}
		
		//博文详情
		public function getArticle($id){
			$list=M()->query('SELECT Id,Title,Content,ViewCount,Tags,TrueName,CreateUser,CommentCount,CreateDate FROM bloglog WHERE id='.$id);
			return $list;
		}
		
		//增加浏览量
		public function getViewCount($id){
			$viewcount=M()->query("UPDATE bloglog SET ViewCount = ViewCount+1 WHERE Id={$id}");
		}
		
		//获取全部博文
		public function getBlogList(){
			$list=M()->query("SELECT Id,Title,Content,ViewCount,Tags,TrueName,CreateUser,CommentCount,CreateDate,IsDeleted FROM bloglog WHERE IsDeleted=0 ORDER BY Id desc");
			cache(array('type'=>'File','expire'=>60));
			cache('list',$list);
			return $list;
		}
		
		//获取相关博文
		public function getAboutBlog($tags,$id){
			$list=M()->query('SELECT Id,Title,Tags,TrueName FROM bloglog WHERE Tags LIKE "%'.$tags.'%" AND Id <> '.$id.' LIMIT 10 ');
			return $list;
		}
		
		//获取分页列表
		public function pageList(){
			$page = $this->page;		//当前页号
			if(!($pinfo = cache('list'))){
				$pinfo = $this->getBlogList();
			}
			$count = count($pinfo);		//总记录数
			$this->count = $count;
			$page=($page<1)?1:(($page>ceil($count/$this->evelpage))?ceil($count/$this->evelpage):$page);
			$t=(($page-1)*($this->evelpage));
			for($i=0;$i<($this->evelpage);$i++){
				if($pinfo[$t+$i])
					$log[$i]=$pinfo[$t+$i];
			}
			$this->bplist = $log;
			$this->pagenum = ceil($count/$this->evelpage);
		}
		
		//判断分页数受否超出
		public function getpagefor(){
			//区间
			$st=floor(($this->page)/10)*10;
			for($i=$st+1;$i<=$st+10;$i++){
				if($i>$this->pagenum){
					continue;
				}
				$lnum[]=$i;
			}
			return $lnum;
		}
		
	}
?>